package filex

import (
	"math/rand"
	"strconv"
	"time"
)

// UploadFile 上传文件
type UploadFile struct {
	Filepath string // /home/htdocs/hk591upload/house/2024/01/02/
	FullPath string // /home/htdocs/hk591upload/house/2024/01/02/src_170417075337478407.jpg
	Pathname string // 2024/01/02/
	Filename string // src_170417075337478407.jpg
	UrlPath  string // house/2024/01/02/src_170417075337478407.jpg
}

// GetUploadPath 获取上传路径
func GetUploadPath(dir, category, ext string) *UploadFile {
	pathname := time.Now().Local().Format("2006/01/02/")

	// 生成文件名
	timestamp := time.Now().UnixMicro()
	randomSuffix := rand.Intn(1000)
	filename := "src_" + strconv.FormatInt(timestamp, 10) + strconv.Itoa(randomSuffix) + ext

	return &UploadFile{
		Filepath: dir + "/" + category + "/" + pathname,
		FullPath: dir + "/" + category + "/" + pathname + filename,
		Pathname: pathname,
		Filename: filename,
		UrlPath:  category + "/" + pathname + filename,
	}
}
